package com.kingdee.eas.ma.control.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.ctrl.kdf.table.IRow;
import com.kingdee.bos.ctrl.swing.KDWorkButton;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.query.SQLExecutorFactory;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.bos.ui.face.IUIWindow;
import com.kingdee.bos.ui.face.UIFactory;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.uiframe.client.UIModelDialog;
import com.kingdee.eas.basedata.org.CtrlUnitInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.OprtState;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.eas.common.client.UIContext;
import com.kingdee.eas.common.client.UIFactoryName;
import com.kingdee.eas.custom.basedata.BudgetAccoInfo;
import com.kingdee.eas.custom.basedata.client.PopupListUI;
import com.kingdee.eas.ma.control.BgControlItemMapCollection;
import com.kingdee.eas.ma.control.BgControlItemMapEntryCollection;
import com.kingdee.eas.ma.control.BgControlItemMapFactory;
import com.kingdee.eas.ma.control.BgControlItemMapInfo;
import com.kingdee.eas.ma.control.BgControlRuleCollection;
import com.kingdee.eas.ma.control.BgControlSchemeCollection;
import com.kingdee.eas.ma.control.BgControlSchemeFactory;
import com.kingdee.eas.ma.control.BgControlSchemeInfo;
import com.kingdee.eas.ma.control.BgControlSettingCollection;
import com.kingdee.eas.ma.control.BgControlSettingFactory;
import com.kingdee.eas.ma.control.BgControlSettingInfo;
import com.kingdee.eas.util.SysUtil;
import com.kingdee.eas.util.client.EASResource;
import com.kingdee.eas.util.client.MsgBox;
import com.kingdee.jdbc.rowset.IRowSet;

public class BgControlSchemeListUICTEx extends BgControlSchemeListUI{

	public BgControlSchemeListUICTEx() throws Exception {
		super();
		
	}
	
	@Override
	public void onLoad() throws Exception {
		super.onLoad();

		KDWorkButton btnExamine = new KDWorkButton("检查");
		btnExamine.setIcon(EASResource.getIcon("imgTbtn_citetree"));//图标
		btnExamine.setVisible(true);
		btnExamine.setEnabled(true);
		this.toolBar.add(btnExamine);//工具栏
		
		
		btnExamine.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try {
					examine();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				
			}
		});
		
		
		  

		  
	}
	
	@SuppressWarnings("unchecked")
	private void examine() throws Exception {
	
	CtrlUnitInfo  company= SysContext.getSysContext().getCurrentCtrlUnit();//获取当前管理单元CU ,比财务组织'成本中心更上一级  	
	String companyId = company.getId().toString();		
	
	//表名
	String sql1="select bgScheme.FRefCube as a from  T_MBG_BgScheme bgScheme  where bgScheme.FOrgUnitID='"+companyId+"' and FIsFormal = '1'"; 	
	IRowSet ra = SQLExecutorFactory.getRemoteInstance(sql1).executeSQL();
    ra.next();
    String as = ra.getString("a");

//		EntityViewInfo view = new EntityViewInfo(); 
//		FilterInfo filter = new FilterInfo();
//		filter.getFilterItems().add(new FilterItemInfo("cu.id", companyId));
		
//		SelectorItemCollection sic = new SelectorItemCollection();
//		sic.add(new SelectorItemInfo("*"));
//		sic.add(new SelectorItemInfo("itemMaps.*"));
//		sic.add(new SelectorItemInfo("itemMaps.entry.*"));		
//		view.setSelector(sic);				
//		BgControlSchemeCollection bac = BgControlSchemeFactory.getRemoteInstance().getBgControlSchemeCollection(view);
       //预算项目
    	//sql Server数据库
		String sql_sqlServer="/*dialect*/ select  p.FNumber as pnum,p.FName_L2 as budgetItem from "+as+" d "+
		" join T_BD_Project p on p.FNumber=SUBSTRING(SUBSTRING(d.FFormula,CHARINDEX(',',d.FFormula)+1,LEN(d.FFormula)-LEN(substring(d.FFormula,0,charindex(',',d.FFormula)))), "+ 
		" CHARINDEX(':',SUBSTRING(d.FFormula,CHARINDEX(',',d.FFormula)+1,LEN(d.FFormula)-LEN(substring(d.FFormula,0,charindex(',',d.FFormula)))))+1 , "+
		" CHARINDEX(',',SUBSTRING(d.FFormula,CHARINDEX(',',d.FFormula)+1,LEN(d.FFormula)-LEN(substring(d.FFormula,0,charindex(',',d.FFormula)))))-CHARINDEX(':',SUBSTRING(d.FFormula,CHARINDEX(',',d.FFormula)+1,LEN(d.FFormula)-LEN(substring(d.FFormula,0,charindex(',',d.FFormula)))))-2) "; 
		
    	//Oracle 数据库
		String sql="/*dialect*/ select  p.FNumber as pnum,p.FName_L2 as budgetItem from "+as+" d "+ 
		" join T_BD_Project p on p.FNumber=SUBSTR(SUBSTR(d.FFormula,INSTR(d.FFormula,',')+1,LENGTH(d.FFormula)-LENGTH(SUBSTR(d.FFormula,0,INSTR(d.FFormula,',')))), "+
		" INSTR(SUBSTR(d.FFormula,INSTR(d.FFormula,',')+1,LENGTH(d.FFormula)-LENGTH(SUBSTR(d.FFormula,0,INSTR(d.FFormula,',')))),':')+1 , "+   
        " INSTR(SUBSTR(d.FFormula,INSTR(d.FFormula,',')+1,LENGTH(d.FFormula)-LENGTH(SUBSTR(d.FFormula,0,INSTR(d.FFormula,',')))),',')-INSTR(SUBSTR(d.FFormula,INSTR(d.FFormula,',')+1,LENGTH(d.FFormula)-LENGTH(SUBSTR(d.FFormula,0,INSTR(d.FFormula,',')))),':')-2) "; 

		
		IRowSet rs = null;
		Boolean isOracle=true;
		try {
			rs = SQLExecutorFactory.getRemoteInstance(sql).executeSQL();
		} catch (Exception e) {
			rs = SQLExecutorFactory.getRemoteInstance(sql_sqlServer).executeSQL();
			isOracle = false;
		}
		
		//sql Server数据库
		String sq_sqlServer="/*dialect*/ select bgs.FNumber as bgsnum,p.FNumber as pnum,bgs.FName_L2 as names,p.FName_L2 as Item from T_MBG_BgControlItemMapEntry b "+
" join T_MBG_BgControlItemMap bg on b.FMapID = bg.FID join T_MBG_BgControlScheme bgs on bgs.FID = bg.FControlSchemeID "+ 
" join T_BD_Project p on p.FNumber =  SUBSTRING(b.FMbgItemValue,CHARINDEX('|',b.FMbgItemValue)+1,LEN(b.FMbgItemValue)-CHARINDEX('|',b.FMbgItemValue)) "+
" where bgs.FControlUnitID= '"+companyId+"' ";
		
		//Oracle 数据库
		String sq="/*dialect*/  select bgs.FNumber as bgsnum,p.FNumber as pnum,bgs.FName_L2 as names,p.FName_L2 as Item from T_MBG_BgControlItemMapEntry b "+
		" join T_MBG_BgControlItemMap bg on b.FMapID = bg.FID join T_MBG_BgControlScheme bgs on bgs.FID = bg.FControlSchemeID  "+
	    " join T_BD_Project p on p.FNumber =  SUBSTR(b.FMbgItemValue,INSTR(b.FMbgItemValue,'|')+1,LENGTH(b.FMbgItemValue)-INSTR(b.FMbgItemValue,'|')) "+ 
		"where bgs.FControlUnitID= '"+companyId+"' "; 
		
		IRowSet rsl = null;
		if (isOracle) {
			rsl = SQLExecutorFactory.getRemoteInstance(sq).executeSQL();
		} else {
			rsl = SQLExecutorFactory.getRemoteInstance(sq_sqlServer).executeSQL();
		}
		Map<String,Map<String, String>> map = new HashMap<String, Map<String,String>>();
	
		while (rsl.next()) {
			String key = rsl.getString("names");
			Map<String, String> map1=new HashMap<String, String>();
			String key1 = rsl.getString("pnum");		
		    map1.put(key1, rsl.getString("Item"));	

		    if(map.containsKey(key)){
		    	Map<String, String> map11 = map.get(key);
		    	map11.put(key1, rsl.getString("Item"));
		    }else{
		    	 map.put(key,map1);
		    }
			 
		}
		
		
		List<String> list = new ArrayList<String>();
		
		while (rs.next()) {	
			for(String key:map.keySet()){
	    	 Map<String, String> map1=map.get(key); //控制策略

					 if(map1.containsKey(rs.getString("pnum"))){
						
					 }else {
						String sea = key+"+"+rs.getString("budgetItem")+"*"+rs.getString("pnum");
						list.add(sea);
						
	    		  }
			}
	    	  
	   }
       
		
	if(list.size() == 0 || null == list){
		MsgBox.showInfo("检查通过");
	}else{
	
		try {			   
			  Map ctx = new UIContext(this);//可以在Map  里面设置要显示的数据
		      
		      IUIWindow uiWindow=null;
		      String ErrorViewUIClass = "com.kingdee.eas.custom.basedata.client.PopupListUI";
		      // ErrorViewUIClass  为弹出提示框具体路径
		      try {
		       uiWindow = (UIModelDialog) UIFactory.createUIFactory(UIFactoryName.MODEL).create( ErrorViewUIClass, ctx, null, OprtState.VIEW);
		       PopupListUI evi = (PopupListUI) uiWindow.getUIObject();
		       evi.fillData(list);  //传输数据
		       uiWindow.show();

		   } catch (Exception e2) {
		    e2.printStackTrace();
		   }
		      
		  } catch (Exception e2) {
		   e2.printStackTrace();
		  }
	}
		
//		for (int i = 0; i < bac.size(); i++) {
//			 BgControlSchemeInfo bai = bac.get(i);
//			 BgControlItemMapCollection ba = bai.getItemMaps();			
//			 String names = bai.getName().toString();
//			 
//			 for (int j = 0; j < ba.size(); j++) {								 
//				BgControlItemMapInfo bc = ba.get(j);
//				BgControlItemMapEntryCollection bd = bc.getEntry();
//							
//				 for (int a = 0; a < bd.size(); a++) {
////					 if (bd.get(a).getBizItemAlias() != null) {
////						 MsgBox.showInfo(names+"第"+a+"行费用类型不能为空!!");
////						 SysUtil.abort();//终止
////					 }
////					 if (bd.get(a).getBizItemValue() == null) {
////						 MsgBox.showInfo(names+"第"+a+"行项目不能为空!!");
////						 SysUtil.abort();//终止
////					 }
////					 if (bd.get(a).getMbgItemAlias() == null) {
////						 MsgBox.showInfo(names+"第"+a+"行预算科目_投资性不能为空!!");
////						 SysUtil.abort();//终止
////					 }
//				
//			      
//					 while (rs.next()) {
//					  if (bd.get(a).getMbgItemValue().toString().equals(rs.getString("budgetItem").toString())) {
//							 
//					  	}else {
//					  		MsgBox.showInfo(names+"项目"+rs.getString("budgetItem")+"没有维护");
//							 SysUtil.abort();
//						}
//				  } 
//				 }
//			  }
//		}
	
	}
}
			 
			 
			 
			 

		
		
	
	
	
	
	


				
			
		
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	


